package com.triones.yjj.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtil;
import com.triones.framework.common.util.StringUtil;
import com.triones.yjj.entity.*;
import com.triones.yjj.entity.bo.AudioBO;
import com.triones.yjj.entity.bo.BackCompareParams;
import com.triones.yjj.entity.bo.IndustryParams;
import com.triones.yjj.entity.vo.*;
import com.triones.yjj.mapper.AudioMapper;
import com.triones.yjj.mapper.BackCompareMapper;
import com.triones.yjj.mapper.IndustryMapper;
import com.triones.yjj.mapper.SubMchMapper;
import com.triones.yjj.service.ToolService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 工具 service 业务实现
 *
 * @Author: triones
 */
@Service
@RequiredArgsConstructor
public class ToolServiceImpl implements ToolService {

    private final AudioMapper audioMapper;
    private final BackCompareMapper backCompareMapper;
    private final IndustryMapper industryMapper;
    private final SubMchMapper subMchMapper;

    @Override
    public TableDataInfo<AudioVO> musicPageList(AudioBO bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        LambdaQueryWrapper<AudioDO> wrapper = Wrappers.lambdaQuery(AudioDO.class);
        wrapper.eq(AudioDO::getType,bo.getType()).eq(AudioDO::getIsPublic,1).or()
        .eq(AudioDO::getType,bo.getType()).eq(AudioDO::getUserId,loginUser.getUserId());
        wrapper.orderByDesc(AudioDO::getCreateTime);
        Page<AudioVO> result = audioMapper.selectVoPage(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }

    @Override
    public Boolean addMusic(AudioDO dto) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        if (dto.getUrl() == null) {
            throw new ServiceException("音频文件地址不能为空");
        }
        if (dto.getName() == null) {
            throw new ServiceException("音频文件名称不能为空");
        }
        dto.setCreateTime(DateUtil.getDateTimeString());
        dto.setUserId(loginUser.getUserId());
        return audioMapper.insert(dto) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importBack(List<BackCompareImportVo> list) {
        List<BackCompareDO> addList = new ArrayList<>();
        for (BackCompareImportVo backCompareImportVo : list) {
            BackCompareDO add = new BackCompareDO();
            add.setBackName(backCompareImportVo.getBackName());
            add.setInterbankNo(backCompareImportVo.getInterbankNo());
            add.setCreateTime(DateUtil.getDateTime());
            addList.add(add);
            System.out.println("add = " + add);
        }
        backCompareMapper.insertBatch(addList);
    }

    @Override
    public TableDataInfo<BackCompareVO> backComparePageList(BackCompareParams params, PageQuery pageQuery) {
        LambdaQueryWrapper<BackCompareDO> lqw = Wrappers.lambdaQuery(BackCompareDO.class);
        if (params.getRegions() != null && params.getRegions().size() > 0) {
            for (String region : params.getRegions()) {
                if (region.length() <= 2) {
                    lqw.like(BackCompareDO::getBackName,region);
                    lqw.or();
                } else {
                    lqw.like(BackCompareDO::getBackName,region.substring(0, region.length() -1));
                    lqw.or();
                }
            }
        }
        lqw.like(ObjectUtil.isNotEmpty(params.getBackName()),BackCompareDO::getBackName,params.getBackName());
        lqw.like(ObjectUtil.isNotEmpty(params.getKeyword()),BackCompareDO::getBackName,params.getKeyword());
        Page<BackCompareVO> result = backCompareMapper.selectVoPage(pageQuery.build(),lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<IndustryVO> industryList(IndustryParams params) {
        LambdaQueryWrapper<IndustryDO> lqw = Wrappers.lambdaQuery(IndustryDO.class);
        lqw.eq(ObjectUtil.isNotEmpty(params.getMainBodyType()),IndustryDO::getMainBodyType,params.getMainBodyType());
        return industryMapper.selectVoList(lqw);
    }

    @Override
    public SubMchVO getSubMchInfo() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        LambdaQueryWrapper<SubMchDO> lqw = Wrappers.lambdaQuery(SubMchDO.class);
        lqw.eq(SubMchDO::getUserId,loginUser.getUserId());
        return subMchMapper.selectVoOne(lqw);
    }

    @Override
    public Boolean updateSubMch(SubMchDO dto) {
        SubMchDO subMchDO = subMchMapper.selectById(dto.getId());
        if (subMchDO == null) {
            throw new ServiceException("子商户信息不存在");
        }
        return subMchMapper.updateById(dto) > 0;
    }

    @Override
    public Boolean saveSubMch(SubMchDO dto) {
        return subMchMapper.insert(dto) > 0;
    }
}
